package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.mapper.MenuMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 30821 on 2018/7/11.
 */
@Service
public class MenuServiceImpl implements IMenuService{
    @Autowired
    private MenuMapper mapper;

    public void save(Menu menu) {
        if (mapper.insert(menu) < 1) {
            throw new LogicException("添加失败");
        }
    }

    public void delete(Long id) {
        if (mapper.deleteByPrimaryKey(id) < 1) {
            throw new LogicException("删除失败");
        }
    }

    public void update(Menu menu) {

        if (mapper.updateByPrimaryKey(menu) < 1) {
            throw new LogicException("修改失败");
        }
    }

    public Menu getById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Menu> listAll() {
        return mapper.selectAll();
    }

    public PageResult queryPage(QueryObject qo) {
        PageResult page = new PageResult();
        int count = mapper.queryCount(qo);
        if (count == 0) {
            return page;
        }
        List<Menu> list = mapper.queryPageData(qo);
        page.setRows(list);
        page.setTotal(count);
        return page;
    }

    //获取菜单   只获取两层  并获取权限
    public List<Menu> getNodes() {
        List<Menu> all = mapper.getNode(null);
        for (Menu zm : all) {
            zm.setChildren(mapper.getNode(zm.getId()));
            for(Menu m2 : zm.getChildren()){
                m2.setChildren(mapper.getNode(m2.getId()));
            }
        }
        return all;
    }
}
